<template>
	<view class="container">
		<ChooseType :typeId="currentTypeId" :typeName="currentTypeName" @changeType="changeType"></ChooseType>
		<uni-forms ref="myForm" label-position="top">
			<uni-forms-item label="标题" >
				<uni-easyinput type="textarea" autoHeight v-model="formData.title" placeholder="标题" />
			</uni-forms-item>
			<uni-forms-item label="内容" >
				<uni-easyinput type="textarea" autoHeight v-model="formData.content" placeholder="内容" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="submit()">保存</button>
	</view>
</template>

<script>
	import {saveNoteTitle} from '@/api/note.js'
	import ChooseType from '@/components/ChooseType.vue'
	
	export default{
		components: {
			ChooseType, 
		},
		data(){
			return {
				// 基础表单数据
				formData: {
					title: '',
					content: ''
				},
				currentTypeName:"",
				currentTypeId:"",
			}
		},
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			// console.log("new.vue   onLoad()   option:",option); //
			this.currentTypeId = option.typeId
			this.currentTypeName = option.typeName
		},
		onBackPress:function(event){
			// 点击后退按钮，此时最多只是添加分类，所以只更新分类即可
			uni.setStorageSync('index_ajax_type', "update_notetypes")
			return false;
			// if (event.from === 'navigateBack') {  
			// 	return false;  
			// }  
			// uni.navigateBack({
			// 	delta: 1,
			// 	success: () => {
			// 		const eventChannel = this.getOpenerEventChannel();
			// 		eventChannel.emit('navReloadEvent');
			// 	}
			// })
			// return true
		},
		methods:{
			submit() {
				if(this.formData.title === ""){
					this.formData.title = this.formData.content.slice(0,20)
				}
				// if(this.formData.content === ""){
				// 	this.formData.content = this.formData.title
				// }
				
				saveNoteTitle(this.formData.title, this.formData.content, this.currentTypeId).then(res => {
					if(res.code != 0){
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						return
					}else{
						
						console.log("tid:",res.data.tid)
						uni.redirectTo({
							url: `list?tid=${res.data.tid}`,
						});
					}
					
				})
			},
			changeType(typeId,typeName){
				// console.log("new changeType:",typeId,typeName)
				this.currentTypeId = typeId
				this.currentTypeName = typeName
			}
		}
	}
</script>

<style>
	.container{
		padding: 10px;
	}
	.scroll-view {
		/* #ifndef APP-NVUE */
		width: 100%;
		height: 100%;
		/* #endif */
		flex: 1
	}
	
	.content-box {
		flex: 1;
		/* #ifdef APP-NVUE */
		justify-content: center;
		/* #endif */
		height: 44px;
		line-height: 44px;
		padding: 0 15px;
		position: relative;
		background-color: #fff;
		border-bottom-color: #f5f5f5;
		border-bottom-width: 1px;
		border-bottom-style: solid;
	}
	
	.content-text {
		font-size: 15px;
	}
	
	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding: 10px 0;
		background-color: #fff;
	}
	
	.button {
		border-color: #e5e5e5;
		border-style: solid;
		border-width: 1px;
		padding: 4px 8px;
		border-radius: 4px;
	}
	
	.button-text {
		font-size: 15px;
	}
	
	.slot-button {
		/* #ifndef APP-NVUE */
		display: flex;
		height: 100%;
		/* #endif */
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 0 20px;
		background-color: #ff5a5f;
	}
	.edit-button {
		/* #ifndef APP-NVUE */
		display: flex;
		height: 100%;
		/* #endif */
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 0 20px;
		background-color: #007aff
	}
	
	.slot-button-text {
		color: #ffffff;
		font-size: 14px;
	}
</style>